package com.dajie.pt.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dajie.common.DictionaryServicesContext;
import com.dajie.common.dictionary.service.DictService;
import com.dajie.common.util.StringUtil;
import com.dajie.infra.user.InfraUserServiceContext;
import com.dajie.infra.user.service.UserBaseService;
import com.dajie.pt.controller.InvitationBaseController;
import com.dajie.pt.file.FileOperateUtil;
import com.dajie.pt.model.InvitationApply;
import com.dajie.pt.service.InvitationApplyService;

@Controller
@RequestMapping("/admin/resume")
public class AdminResumeController extends InvitationBaseController
{

	@Autowired
	InvitationApplyService<InvitationApply, Integer> invitationApplyService;

	static DictService dictService = DictionaryServicesContext.getInstance().getDictService();

	UserBaseService userService = InfraUserServiceContext.getInstance().getUserService();

	static final String ftlPre = "/admin/resume/";

	private ModelAndView getMV(String view)
	{
		return new ModelAndView(ftlPre + view);
	}

	@RequestMapping("/showlist")
	public ModelAndView showList(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "status", required = false) Integer status,
			@RequestParam(value = "id", required = false) Integer id,
			@RequestParam(value = "mobile", required = false) String mobile,
			@RequestParam(value = "jid", required = false) String jid)
	{
		ModelAndView mv = getMV("showlist");
		Map<String, Object> params = new HashMap<String, Object>();
		if (status == null)
		{
			status = null;
		} else if (status == 0)
		{
			status = null;
		}
		params.put("status", status);
		if (id != null)
		{
			params.put("id", id);
		}
		if (StringUtil.isNotEmpty(mobile))
		{
			params.put("mobile", mobile.trim());
		}
		if (StringUtil.isNotEmpty(jid))
		{
			params.put("jid", jid.trim());
		}
		
		List<InvitationApply> list = new ArrayList<InvitationApply>();
		if(params.size() > 0)
		{
//			list = invitationApplyService.findByPage(params);
			list = invitationApplyService.showlist(params);
		}
		mv.addObject("list", list);
		mv.addObject("status", status);
		mv.addObject("id", id);
		mv.addObject("mobile", mobile);
		mv.addObject("jid", jid);
		return mv;
	}

	@RequestMapping("/genresume")
	@ResponseBody
	public String genResume(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) Integer id)
	{

		this.getClass().getResource("").getPath();
		Map<String, Object> map = new HashMap();
		map.put("id", id);
		InvitationApply invitationApply = invitationApplyService.findByPage(map).get(0);
		// invitationApply.getBasicInfo().setPhoto(
		// userService.getUserByUid(invitationApply.getUid()).getAvatarUrl("m"));
		String path = FileOperateUtil.getUploadDir(request) + invitationApply.getBasicInfo().getName();
		if (StringUtil.isNotEmpty(invitationApply.getItskillsDesc()) && invitationApply.getItskillsDesc().contains("限400字"))
		{
			invitationApply.setItskillsDesc("");
		}
		if (StringUtil.isNotEmpty(invitationApply.getSelfEvaluation()) && invitationApply.getSelfEvaluation().contains("限400字"))
		{
			invitationApply.setSelfEvaluation("");
		}
		LOGGER.error("path:{}", path);
		return invitationApply.getBasicInfo().getName();
	}

	@RequestMapping(value = "genresumeall")
	@ResponseBody
	public String genResumeAll(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "ids", required = true) String ids) throws Exception
	{
		String ctxDlPath =  FileOperateUtil.getUploadDir(request);
		String ctxPath = ctxDlPath + "resumeall";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		List<InvitationApply> list = invitationApplyService.findByPage(params);
		Runtime.getRuntime().exec("rm -rf " + ctxPath);
		Runtime.getRuntime().exec("mkdir " + ctxPath);

		for (InvitationApply ia : list)
		{
			if (StringUtil.isNotEmpty(ia.getResumeFile()))
			{
				if(!ia.getResumeFile().startsWith("local"))
				{
					LOGGER.info("shell command:{}",
							"wget " + ia.getResumeFile() + " -O " + ctxPath + "/" + ia.getBasicInfo().getName()
							+ ia.getResumeFile().substring(ia.getResumeFile().lastIndexOf('.')));
					Runtime.getRuntime().exec(
						"wget " + ia.getResumeFile() + " -O " + ctxPath + "/" + ia.getBasicInfo().getName()
								+ ia.getResumeFile().substring(ia.getResumeFile().lastIndexOf('.')));
				}else if(ia.getResumeFile().startsWith("local"))
				{
					LOGGER.info("shell command:{}",
							"cp " + ctxDlPath +ia.getBasicInfo().getName()
							+ ia.getResumeFile().substring(ia.getResumeFile().lastIndexOf('.')) + " " + ctxPath + "/" + ia.getBasicInfo().getName()
							+ ia.getResumeFile().substring(ia.getResumeFile().lastIndexOf('.')));
					Runtime.getRuntime().exec(
							"cp " + ctxDlPath + ia.getBasicInfo().getName()
							+ ia.getResumeFile().substring(ia.getResumeFile().lastIndexOf('.')) + " " + ctxPath + "/" + ia.getBasicInfo().getName()
									+ ia.getResumeFile().substring(ia.getResumeFile().lastIndexOf('.')));
				}
					
			} else
			{
				try
				{
				} catch (Exception e)
				{
					LOGGER.error("generate resume error for {}({}):{}",ia.getBasicInfo().getName(),ia.getMobile(),e);
				}
			}
		}

		return ctxDlPath + "resumeall.zip";
	}

	@RequestMapping(value = "getattr")
	@ResponseBody
	public String getAttr(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "ids", required = true) String ids) throws Exception
	{
		String ctxPath = FileOperateUtil.getUploadDir(request);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		List<InvitationApply> list = invitationApplyService.findByPage(params);
		InvitationApply ia = null;
		if (list != null && list.size() > 0)
		{
			ia = list.get(0);
			if (StringUtil.isNotEmpty(ia.getResumeFile()))
			{
				if(!ia.getResumeFile().startsWith("local"))
				{
					Runtime.getRuntime().exec(
						"wget " + ia.getResumeFile() + " -O " + ctxPath + ia.getBasicInfo().getName()
								+ ia.getResumeFile().substring(ia.getResumeFile().lastIndexOf('.')));

				}
			} else
			{
			}
			LOGGER.error("getresumeinattr:" + ctxPath + ia.getBasicInfo().getName()
					+ ia.getResumeFile().substring(ia.getResumeFile().lastIndexOf('.')));
			return ctxPath + ia.getBasicInfo().getName()
					+ ia.getResumeFile().substring(ia.getResumeFile().lastIndexOf('.'));
		} else
		{
			return "error";
		}

	}

}